package util

import (
	"context"
	"fmt"
	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"
	"log"
	"time"
)

var mgdbConn *mongo.Client

// Database 测试数据库
const Database = "jim"

// Collection 测试Collection
const Collection = "demo_user"

// InitMgdb 初始化MongoDB数据库连接
func InitMgdb() {
	serverAPIOptions := options.ServerAPI(options.ServerAPIVersion1)
	clientOptions := options.Client().
		ApplyURI("mongodb+srv://ubt_db_usr:01235689!Hh@cluster0.utwvo0c.mongodb.net/?retryWrites=true&w=majority").
		SetServerAPIOptions(serverAPIOptions)
	ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
	defer cancel()
	conn, err := mongo.Connect(ctx, clientOptions)
	if err != nil {
		log.Fatal(err)
	}
	mgdbConn = conn
}

// InstallOne 插入一行数据
func InstallOne() {
	collection := mgdbConn.Database(Database).Collection(Collection)
	one, err := collection.InsertOne(context.TODO(), DemoUser{
		Id:    time.Now().UnixMicro(),
		Name:  "张三",
		Phone: "18346566786",
		Age:   28,
	})
	if err != nil {
		panic(err)
	}
	fmt.Println(*one)
	ListAll()
}

// ListAll 查询全部数据
func ListAll() {
	collection := mgdbConn.Database(Database).Collection(Collection)
	var result []DemoUser
	cursor, _ := collection.Find(context.TODO(), bson.D{})
	if cursor.All(context.TODO(), &result) != nil {
		fmt.Println("数据查询错误")
	}
	for i, user := range result {
		UserFmt(i+1, user)
	}
}

// InstallMany 批量插入
func InstallMany() {
	collection := mgdbConn.Database(Database).Collection(Collection)
	users := []interface{}{
		DemoUser{Id: 1, Name: "汪鸣晨", Phone: "13659630001", Age: 28},
		DemoUser{Id: 2, Name: "盖流惠", Phone: "13659630004", Age: 28},
		DemoUser{Id: 3, Name: "桓野云", Phone: "13659630004", Age: 28},
		DemoUser{Id: 4, Name: "郝语彤", Phone: "18345654901", Age: 28},
		DemoUser{Id: 5, Name: "沈振文", Phone: "18345654902", Age: 28},
		DemoUser{Id: 6, Name: "古雪翎", Phone: "18345654903", Age: 28},
		DemoUser{Id: 7, Name: "燕丁辰", Phone: "18345654904", Age: 28},
		DemoUser{Id: 8, Name: "乔任真", Phone: "18345654905", Age: 28},
		DemoUser{Id: 9, Name: "暴笑妍", Phone: "18345654906", Age: 28},
		DemoUser{Id: 10, Name: "池雁风", Phone: "18345654907", Age: 28},
		DemoUser{Id: 11, Name: "马玉萍", Phone: "18345654908", Age: 28},
		DemoUser{Id: 12, Name: "崔子舒", Phone: "18345654909", Age: 28},
		DemoUser{Id: 13, Name: "简晗晗", Phone: "18345654910", Age: 28},
		DemoUser{Id: 14, Name: "邹许洌", Phone: "18345654911", Age: 28},
		DemoUser{Id: 15, Name: "梁晶茹", Phone: "18345654912", Age: 28},
		DemoUser{Id: 16, Name: "刘语林", Phone: "18345654913", Age: 28},
		DemoUser{Id: 17, Name: "曹可可", Phone: "18345654914", Age: 28},
	}
	many, _ := collection.InsertMany(
		context.TODO(),
		users,
	)
	fmt.Println(many)
}

// UserFmt 格式化输出
func UserFmt(index int, user DemoUser) {
	fmt.Printf("%d --姓名: %s -- 手机号: %s --- 年龄: %d--\n", index, user.Name, user.Phone, user.Age)
}

// DemoUser 测试用户struct
type DemoUser struct {
	Id    int64  `bson:"_id"`
	Name  string `bson:"name"`
	Phone string `bson:"phone"`
	Age   int64  `bson:"age"`
}
